//1.引入Vue 
import Vue from "vue";

//2.引入Vuex
import Vuex from "vuex";

//引入uuid
import {v4 as uuidv4} from "uuid"

//3.使用插件
Vue.use(Vuex);


//创建数据
const state = {
    movieList: [],
    todoList: [
        {id: uuidv4() , thing: "1" , done: false},
        {id: uuidv4() , thing: "2" , done: false},
        {id: uuidv4() , thing: "3" , done: false},
    ]
}

//创建Mutations
const mutations = {
    //1.把电影列表数组赋值到state中
    addMovie(state , payload){
        state.movieList = payload
    },

    //2.添加todo
    addTodo(state , payload){
        state.todoList.unshift(payload)
    },

    //3.单个切换
    asignChange(state , id){
        state.todoList = state.todoList.map((item) => {
            return item.id === id ? {...item , done:!item.done} : item
        })
    },

    //4.单个删除
    asignDelete(state , id){
        //删除提示
        const isDelete = confirm("确定删除吗?")
        if(!isDelete) return
        
        state.todoList = state.todoList.filter((item) =>{
            return item.id !== id
        })
    },

    //4.全选
    allChecked(state , value){
        state.todoList = state.todoList.map((item) => {
            return {...item , done: value}
        })
    },

    //5.清除已完成
    deleteSuccess(state){
        state.todoList = state.todoList.filter((item) => !item.done )
    }
}

//4.插件store仓库
export default new Vuex.Store({
    state,
    mutations,
})